/* -*-c++-*- */
/* osgGIS - GIS Library for OpenSceneGraph
 * Copyright 2007-2008 Glenn Waldron and Pelican Ventures, Inc.
 * http://osggis.org
 *
 * osgGIS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

#ifndef _OSGGIS_LUA_SCRIPT_ENGINE_H_
#define _OSGGIS_LUA_SCRIPT_ENGINE_H_ 1

#include <osgGIS/Common>
#include <osgGIS/ScriptEngine>
#include <osgGIS/ResourceLibrary>
extern "C" {
#include "lua.h"
}

namespace osgGIS
{
    /* (internal - no api docs)
     *
     * Scripting engine that evalutes LUA scripts.
     */
    class /*INTERNAL*/ Lua_ScriptEngine : public ScriptEngine
    {
    public:

        Lua_ScriptEngine();
        virtual ~Lua_ScriptEngine();


    public: // ScriptEngine

        /**
         * Installs a script into the global namespace. Scripts that run via
         * the run() method will be able to access fuctions etc. installed
         * using this method.
         */
        virtual void install( Script* script );
        
        /**
         * Runs a script and returns the result.
         */
        virtual ScriptResult run( Script* script );

        /**
         * Runs a script from a FeatureFilter batch context and returns the result.
         */
        virtual ScriptResult run( Script* script, FilterEnv* env );

        /**
         * Runs a script from a FeatureFilter context and returns the result.
         */
        virtual ScriptResult run( Script* script, Feature* feature, FilterEnv* env );


    private:
        ScriptList scripts;
        lua_State* L;
    };


    class ResourceList_Lua
    {
    public:
        ResourceList_Lua( const ResourceList& _obj ) : data(_obj) { }
        int size() const { return data.size(); }
        const char* getName(int i) { unsigned int j=(unsigned int)i; return j < data.size()? data[j].get()->getName() : NULL; }
    private:
        ResourceList data;
    };

    class ResourceLibrary_Lua
    {
    public:
        ResourceLibrary_Lua( ResourceLibrary* _lib ) : lib( _lib ) { }
        SkinResourceQuery newSkinQuery() { return lib->newSkinQuery(); }
        ResourceList_Lua getSkins() { return ResourceList_Lua( lib->getSkins() ); }
        ResourceList_Lua getSkins( const SkinResourceQuery& q ) { return lib->getSkins( q ); } 
        const char* getPath( const char* name ) { 
            PathResource* resource = dynamic_cast<PathResource*>( lib->getResource( std::string( name ) ) );
            return resource? resource->getAbsoluteURI().c_str() : NULL; }
    private:
        osg::ref_ptr<ResourceLibrary> lib;
    };
}

#endif // _OSGGIS_SCRIPT_ENGINE_H_ 
